import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-12
 * Time: 15:43
 */
class Person implements Cloneable{
    public int id;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class TestDemo {
    public static void main15(String[] args){
        System.out.println(readFile());
    }
    public static String readFile(){
        // 尝试打开文件, 并读其中的一行.
        File file = new File("d:/test.txt");
        // 使用文件对象构造 Scanner 对象.
        Scanner sc = null;
        //受查异常一般使用try catch - 快捷键
        try {
            sc = new Scanner(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return sc.nextLine();
    }
    public static void func4(int x) throws RuntimeException{
        if (x == 0){
            throw new ArithmeticException("/ 等于0");
            //抛出自定义的异常 - 非系统异常 - bug等
            //throw new RuntimeException("x ==" + x);
            //System.out.println(10 / x);
        }
    }
    public static void main14(String[] args) {
        try {
            func4(0);
        } catch (ArithmeticException e){
            e.printStackTrace();
        }
        //func4(0);
    }

    //尽量避免在finally中使用return!!!
    public static int func3(){
        int a = 10;
        try {
            return a;
        }catch (ArithmeticException e){
            e.printStackTrace();
        } finally {
            return 120;
        }
    }
    public static void main(String[] args) {
        System.out.println(func3());
    }
    //throws告诉调用这函数的人可能会抛异常
    public static void func2(int n) throws ArithmeticException{
        System.out.println(10/n);
    }
    public static void main12(String[] args) {
        try {
            func2(0);
        } catch (ArithmeticException e){
            e.printStackTrace();
        }
        func2(0);
        System.out.println("gawei");
    }
    public static void main11 (String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int n = scanner.nextInt();
            System.out.println(10 / n);
        } catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("输入有误");
        }  finally {
            //一般用作资源的关闭 - finally不管有没有异常都会执行
            System.out.println("finally执行了");
        }
    }
    public static void main10(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int n = scanner.nextInt();
            System.out.println(10 / n);
        } catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("输入有误");
        } catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("算术异常");
        } finally {
            //一般用作资源的关闭 - finally不管有没有异常都会执行
            System.out.println("finally执行了");
        }
    }
    public static void main9(String[] args) {
        //catch 在捕获异常时应保证子类在前，父类在后，否则报错
        /*int[] array = {1,2,3};
        try {
            array = null;
            System.out.println(array[5]);
            System.out.println("haha");
        }catch (IndexOutOfBoundsException e) {//最好不用，范围太广 不好确定
            e.printStackTrace();//异常信息栈
            System.out.println("已捕捉到一个数组越界或者空指针异常");
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        System.out.println("哈喽！！！");*/
    }
    public static void main8(String[] args) {
        int[] array = {1,2,3};
        try {
            array = null;
            System.out.println(array[5]);
            System.out.println("haha");
        }catch (Exception e) {//最好不用，范围太广 不好确定
            e.printStackTrace();//异常信息栈
            System.out.println("已捕捉到一个数组越界或者空指针异常");
        }
        System.out.println("哈喽！！！");
    }
    public static void func(){
        func();
    }
    public static void main7(String[] args) {
        func();
    }
    //当try中发生了异常过后，会交给对应的catch里边，捕捉到对应的异常
    public static void main6(String[] args) {
        int[] array = {1,2,3};
        try {
            array = null;
            System.out.println(array[5]);
            System.out.println("haha");
        }catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
            e.printStackTrace();//异常信息栈
            System.out.println("已捕捉到一个数组越界或者空指针异常");
        }
        System.out.println("哈喽！！！");
    }
    public static void main5(String[] args) {
        int[] array = {1,2,3};
        try {
            array = null;
            System.out.println(array[2]);
            System.out.println("haha");
        }catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();//异常信息栈
            System.out.println("已捕捉到一个数组越界异常");
        } catch (NullPointerException e) {
            e.printStackTrace();
            System.out.println("已捕捉到空指针异常");
        }
        System.out.println("哈喽！！！");
    }
    public static void main4(String[] args) {
        //如果你不处理异常，那么这个异常会交给JVM处理，一旦交给了JVM处理，程序立马终止了
        int[] array = {1,2,3};
        System.out.println(array[5]);
        System.out.println("其它功能!!!");
    }
    public static void main3(String[] args) {
        int[] array = {1,2,3};
        System.out.println(array[5]);
        /*try {
            System.out.println(array[5]);
            System.out.println("haha");
        }catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();//异常信息栈
            System.out.println("已捕捉到一个数组越界异常");
        }*/
        System.out.println("哈喽！！！");
    }
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println(n);
    }
    public static void main1(String[] args) {
        Person person = new Person();
        try {
            Person person1 = (Person) person.clone();
        } catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        /*int[] array = {1,2,3};//异常三 - 越界异常
        System.out.println(array[9]);
        System.out.println(10/0);//异常一 - 算术异常
        String str = null;
        System.out.println(str.length());//异常二 - 空指针异常*/
    }
}